package com.jiazhong.mybatis.m1;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiazhong.commons.bean.Emp;
import com.jiazhong.mybatis.m1.mapper.EmpMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class App {


    private static void a() {
        // 1.加载config.xml
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        // 2.产生会话工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        // 3.生产会话
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 4.产生EmpMapper的对象
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        // 5.调用方法
        List<Emp> list = mapper.findAll();
        // 6.循环集合
        list.forEach(System.out::println);
        // 7.关闭会话
        sqlSession.close();
    }

    private static void b() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.findById(8058);
        System.out.println(emp);
        sqlSession.close();
    }

    private static void c() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> list = mapper.findByName("%张222%");
        list.forEach(System.out::println);
        sqlSession.close();
    }

    private static void d1() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setEname("%张%");
        emp.setJob("程序员");
        List<Emp> list = mapper.findByNameAndJob1(emp);
        list.forEach(System.out::println);
        sqlSession.close();
    }

    private static void d2() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Map map = new HashMap();
        map.put("ename", "%张%");
        map.put("job", "程序员");
        List<Emp> list = mapper.findByNameAndJob2(map);
        list.forEach(System.out::println);
        sqlSession.close();
    }

    private static void d3() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> list = mapper.findByNameAndJob3("%张%", "程序员");
        list.forEach(System.out::println);
        sqlSession.close();
    }


    private static void e1() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        int page = 2;
        List<Emp> list = mapper.findByPage((page - 1) * 10, 10);
        list.forEach(System.out::println);
        sqlSession.close();
    }

    private static void e2() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        int page = 11;
        // 定义第几页以及每页显示条数
        PageHelper.startPage(page, 10);
        // 调用任意的查询操作
        List<Emp> list = mapper.findAll();
        list.forEach(System.out::println);
        // 封装的分页数据
        PageInfo<Emp> pageInfo = new PageInfo<>(list);
        System.out.println("总条数：" + pageInfo.getTotal());
        System.out.println("总页数：" + pageInfo.getPages());
        System.out.println("当前页：" + pageInfo.getPageNum());
        System.out.println("上一页：" + pageInfo.getPrePage());
        System.out.println("下一页：" + pageInfo.getNextPage());
        System.out.println("应当每页显示条数：" + pageInfo.getPageSize());
        System.out.println("实际每页显示条数：" + pageInfo.getSize());
        sqlSession.close();
    }


    private static void f() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setComm(400.0);
        emp.setSal(8888.3);
        emp.setEname("王卓飞");
        emp.setJob("网页设计");
        emp.setMgr(7788);
        emp.setDeptNo(20);
        int row = mapper.insert(emp);
        System.out.println(row);
        // 手动提交事务数据
        sqlSession.commit();
        sqlSession.close();
    }
    private static void g() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setComm(200.0);
        emp.setEmpNo(8058);
        emp.setSal(18888.3);
        emp.setEname("杜晓胜");
        emp.setJob("歌手");
        emp.setMgr(7980);
        emp.setDeptNo(10);
        int row = mapper.update(emp);
        System.out.println(row);
        // 手动提交事务数据
        sqlSession.commit();
        sqlSession.close();
    }

    private static void h() {
        InputStream in = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        int row = mapper.delete(8047);
        System.out.println(row);
        // 手动提交事务数据
        sqlSession.commit();
        sqlSession.close();
    }

    public static void main(String[] args) {
        b();
    }
}